﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.Commands
{
    public class Register : ICommand
    {
        private readonly string m_commandkey = "register";

        public Register()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return "register";
        }

        public void execute(string[] command_parts, Client client)
        {
            if (!client.Nickname.StartsWith("guest")) // he changed his nickname
            {
                if (command_parts.Length > 1)
                {
                    Users.User user = new Users.User();
                    user.Nickname = client.Nickname;

                    if (command_parts[1].Length > 0)
                    {
                        if (Users.UserManager.Instance.addUser(user) != rvalue.USER_ALREADY_EXISTS)
                        {
                            user.Password = command_parts[1];
                            Data.DatabaseWriter.Instance.Queue(user.getFile());
                            MessageExecution.sendData(client, "Your profile has been created.");
                        }
                        else
                            MessageExecution.sendData(client, "An error occured. Your current nickname is already associated with an account.");
                    }
                    else
                        MessageExecution.sendData(client, "Your password can't be empty");
                }
                else
                    MessageExecution.sendData(client, "Please type a password.");
            }
            else
                MessageExecution.sendData(client, "You have a default nickname. Please change it with the \"/nick your_nick_name\" command");
        }
    }
}
